package com.javafactroy.kunlong.domain.generalright.repository;

import com.javafactroy.kunlong.base.model.ReturnPageInfo;
import com.javafactroy.kunlong.data.dto.DrUserDeptApiDto;
import com.javafactroy.kunlong.data.dto.DrUserEmpsApiDto;
import com.javafactroy.kunlong.data.dto.VUserGroupEmpsApiDto;
import com.javafactroy.kunlong.data.entity.DrUserDeptApiEntity;
import com.javafactroy.kunlong.data.entity.DrUserEmpsApiEntity;
import com.javafactroy.kunlong.data.entity.VUserGroupEmpsApiEntity;
import com.javafactroy.kunlong.data.service.*;
import com.javafactroy.kunlong.domain.generalright.agg.DptRightAgg;
import com.javafactroy.kunlong.domain.generalright.agg.UserRightAgg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserRightRepository {

    @Autowired
    DrUserDeptService userDeptService;
    @Autowired
    DrUserGroupService drUserGroupService;

    @Autowired
    VUserGroupEmpsService vUserGroupEmpsService;
    @Autowired
    DrUserEmpsService drUserEmpsService;

    @Autowired
    DptRightRepository dptRightRepository;


    /*
     * @params userId
     * */
    public UserRightAgg load(Long userId) {
        UserRightAgg agg = new UserRightAgg().setUserId(userId);
        loadDeptRight(agg);
        loadUserEmps(agg);
        loadGroupEmps(agg);

        return agg;
    }

    void loadDeptRight(UserRightAgg agg) {
        DrUserDeptApiDto userDeptApiDto=new DrUserDeptApiDto();
        userDeptApiDto.setUserId(agg.getUserId());//.setPageSize(1000 );
        List<DrUserDeptApiEntity> pageInfo = userDeptService.select(userDeptApiDto);
        List<DptRightAgg> dptRightAggs = new ArrayList<>();
        pageInfo.forEach(i -> {

            DptRightAgg dptRightAgg = dptRightRepository.load(i.getDeptId());
            dptRightAggs.add(dptRightAgg);
        });
        agg.setUserDepts(dptRightAggs);
    }

    void loadUserEmps(UserRightAgg agg) {
        ReturnPageInfo<DrUserEmpsApiEntity> emps = drUserEmpsService.query(new DrUserEmpsApiDto().setUserId(agg.getUserId()));
        agg.setUserEmps(emps.getList());
    }

    void loadGroupEmps(UserRightAgg agg) {
        ReturnPageInfo<VUserGroupEmpsApiEntity> emps = vUserGroupEmpsService.query(new VUserGroupEmpsApiDto().setUserId(agg.getUserId()));
       agg.setUserGroupEmps(emps.getList());
    }
}
